<?php
namespace App\Service\Admin\Platform;

class ItemUserService extends BaseService
{
    //项目用户

    /**
     * 列表
     * @param array $pas【cateid业务类型，typeid品类】
     * @param array $pages【start，limit】
     */
    public function index($pas, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbXm)->where('del', 0);
        if ($pas['cate_id']) {
            $db = $db->where('cate_id', $pas['cate_id']);
        }
        if ($pas['type_id']) {
            $db = $db->where('type_id', $pas['type_id']);
        }
        $total = $db->count('id');
        $res = $db->orderBy('id', 'desc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!$total) {
            return $this->toback(-2, '没有数据');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //品类
            $typeOne = (array) $this->dbConn($this->dbMkConfig)
                ->where('id', $v['type_id'])
                ->first();
            //业务类型
            $cateOne = (array) $this->dbConn($this->dbTmpCate)
                ->where('id', $v['cate_id'])
                ->first();
            //用户
            $userOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['uid'])
                ->first();
            $type_cn = '-';
            if (!empty($userOne)) {
                $type_cn = $userOne['cid'] ? '企业' : '个人';
            }
            $arys[]= [
                'id' => $v['id'],
                'title' => $v['title'],
                'type_name' => empty($typeOne) ? '-' : $typeOne['title'],
                'cate_name' => empty($cateOne) ? '-' : $cateOne['title'],
                'acc_name' => empty($userOne) ? '-' : $userOne['uname'],
                'realname' => empty($userOne) ? '-' : $userOne['realname'],
                'user_type_cn' => $type_cn,
                'addtime' => $v['addtime'],
                'item_total' => empty($userOne) ? 0 : $userOne['item_total'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}